import request from '@/utils/request'
import type { AxiosResponse } from 'axios'
import { ElMessage } from 'element-plus'

//定义接口
interface dataType {
  account: string
  password: string
}
interface LoginResponse {
  code: number
  msg: string
}
interface paramsType {
  currentPage: number
  pageSize: number
}
interface fetchUserDelType {
  id: number
}
interface fetchUserEditType {
  id: number
  account: string
  userGroup: string
}
interface fetchUserBatchdel {
  id: string
}
interface fetchUserAddType {
  account: string
  password: string
  userGroup: string
}
interface fetchUserCheckOldPwdType {
  oldPwd: string
}
interface fetchUserEditPwdType {
  newPwd: string
}
interface fetchUserAvatarEditType {
  imgUrl: string
}
interface fetchUserExportType {
  currentPage: number
  pageSize: number
}
interface fetchUserAcc_uploadType {
  file: Blob
}
//二次封装，只需要传入参数，提取url以及处理返回的数据
export const fetchLogin = async (data: dataType) => {
  const res: AxiosResponse<LoginResponse> = await request.post('/users/checkLogin', data)
  if (res.data.code === 0) {
    ElMessage({
      type: 'success',
      message: res.data.msg,
    })
  }
  if (res.data.code === 1) {
    ElMessage({
      type: 'warning',
      message: '账号或密码不正确',
    })
  }
  return res.data
}
export const fetchUserEdit = async (data: fetchUserEditType) => {
  const res = await request.post('/users/edit', data)
  return res.data
}
export const fetchUserAdd = async (data: fetchUserAddType) => {
  const res = await request.post('/users/add', data)
  return res.data
}
export const fetchUserEditPwd = async (data: fetchUserEditPwdType) => {
  const res = await request.post('/users/editpwd', data)
  return res.data
}
export const fetchUserAcc_upload = async (data: fetchUserAcc_uploadType) => {
  const res = await request.post('/users/acc_upload', data)
  return res.data
}
//二次封装，只需传入参数
export const fetchList = async (data: paramsType) => {
  const res = await request.get('/users/list', data)
  return res.data
}
export const fetchUserDel = async (data: fetchUserDelType) => {
  const res = await request.get('/users/del', data)
  return res.data
}
export const fetchUserBatchdel = async (data: fetchUserBatchdelType) => {
  const res = await request.get('/users/batchdel', data)
  return res.data
}
export const fetchUserCheckOldPwd = async (data: fetchUserCheckOldPwdType) => {
  const res = await request.get('/users/checkoldpwd', data)
  return res.data
}
export const fetchUserInfo = async () => {
  const res = await request.getCategories('/users/info')
  return res.data
}
export const fetchUserAvatarEdit = async (data: fetchUserAvatarEditType) => {
  const res = await request.get('/users/avataredit', data)
  return res.data
}
export const fetchUserExport = async (data: fetchUserExportType) => {
  const res = await request.get('/users/export', data)
  return res.data
}
